package com.cpi.cornpp.transports.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.corntypes.exception.CornTypesException;
import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;

public class ShowAddNewTransportAction extends MappingDispatchAction {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	
	public ActionForward createNewTransport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException, CornTypesException {
		TransportView transportView = null;
		CornTypesService service = null;
		List cornTypesList = null;
		HttpSession session = null;
		ActionForward forward = null;		
		try {						
			service = new CornTypesService();
			cornTypesList = new ArrayList(0);
			cornTypesList = service.getListCornTypes();						
			forward = mapping.findForward(FAILURE_FORWARD);
			session = request.getSession(false);
			
			transportView = new TransportView();
			
			session.setAttribute("cornTypesList", cornTypesList);
			session.setAttribute("transportView", transportView);
			request.setAttribute("transportView", transportView);
			
			forward = mapping.findForward(SUCCESS_FORWARD);
			
		}catch (CornTypesException cornTypesException){
			session = request.getSession(true);
			session.setAttribute(CUSTOM_EXCEPTION, cornTypesException);
			throw cornTypesException;
		}catch (Exception exception){
			CornTypesException cornTypesException = null;
			exception.printStackTrace(System.out);
			cornTypesException= new CornTypesException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, cornTypesException);
			throw cornTypesException;
		}
		
		return forward;
	}
		
	public ActionForward showAddTransportCostElement(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException {
		String messageError = null;
		TransportService transportService = null;
		TransportView transportView = null;
		List costElementsView = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			session = request.getSession(false);
			messageResources = getResources(request);
			transportView = (TransportView) session.getAttribute("transportView");
			
			if ((form != null) && (transportView != null)){
				transportService = new TransportService();
				costElementsView = transportService.getCostElements();
								
				session.setAttribute("costElementsList", costElementsView);
				request.setAttribute("costElementsList", costElementsView);
				
				updateNewTransportData((TransportForm) form, request);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			} else {
				forward = mapping.findForward("showTransportQuery");
			}

		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, tre);
				form.reset(mapping, request);
				throw tre;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_LISTS);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return forward;
	}
	
	private void updateNewTransportData(TransportForm transportForm, HttpServletRequest request){
		TransportView transportView = null;
		HttpSession session = null;
		
		if ((transportForm != null) && (request != null)){
			session = request.getSession(false);
			transportView = (TransportView) session.getAttribute("transportView");
			
			transportView.setTransportShortName(transportForm.getCode());
			transportView.setTransportDescription(transportForm.getDescription());
			transportView.setTransportCapacity((StringUtils.isNotEmpty(transportForm.getCapacity())) ? Double.parseDouble(transportForm.getCapacity()) : 0);
			transportView.setTransportPriority((StringUtils.isNotEmpty(transportForm.getPriority())) ? Integer.parseInt(transportForm.getPriority()) : 0);
			transportView.setTransportNomenclature(transportForm.getNomenclature());
			
			session.setAttribute("transportView", transportView);
			request.setAttribute("transportView", transportView);
		}
	}
}